package LayoutBluetooth;

import java.awt.Rectangle;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.bluecove.tester.log.Logger;

import net.sf.bluecove.Configuration;
import net.sf.bluecove.Switcher;
import net.sf.bluecove.se.BlueCoveSpecific;
import net.sf.bluecove.se.FileStorage;
import net.sf.bluecove.se.JavaSECommon;

    /**
     *
     * @author  Symon
     */
    public class SwingtoothStart extends javax.swing.JFrame {
    	/**
		 * 
		 */
    	
		private static final long serialVersionUID = 5055442276712787146L;
		public void windowClosing(WindowEvent w) {
			quit();}
        
        /** Creates new form NewJFrame */
        public SwingtoothStart() {
            initComponents();
        	try {
				JavaSECommon.initOnce();
				updateStatus("Java Initialised");
				Configuration.storage = new FileStorage();
				updateStatus("Storage Appended");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			updateStatus("Ready to Search for Bluetooth Devices!");
        }
        
        /** This method is called from within the constructor to
         * Initialise the form.
         */
        private void initComponents() {
        	
            //add(textField, c);

            //c.fill = GridBagConstraints.BOTH;
            //c.weightx = 1.0;
            //c.weighty = 1.0;
            //add(scrollPane, c);
        


        	
            jPanel1 = new JPanel();
            resultsBox = new JTextField();
            startConnection = new JButton();
            stopSearching = new JButton();
            title = new JLabel();
            listServices = new JButton();
            infoLabel = new JLabel();
            infoUpdate = new JTextArea(5, 20);
            infoUpdate.setEditable(false);

            //Sets the boxes default to disabled
            listServices.setEnabled(false);
            stopSearching.setEnabled(false);

            resultsBox.setText("resultsBox");

            startConnection.setText("Start Searching");
            startConnection.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    startConnectionActionPerformed(evt);
                }
            });

            stopSearching.setText("Stop Searching");
            stopSearching.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    stopSearchingActionPerformed(evt);
                }
            });

            title.setFont(new java.awt.Font("Arista 2.0 Alternate Light", 2, 36));
            title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            title.setText("Blueswing Management System");
            

            listServices.setText("Start Searching");
            listServices.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    listServicesActionPerformed(evt);
                }
            });

            infoLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
            infoLabel.setText("Info:");

            infoUpdate.setFont(new java.awt.Font("Tahoma", 0, 12));
            infoUpdate.setBackground(getBackground());

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(stopSearching)
                            .addComponent(startConnection))
                        .addComponent(listServices))
                    .addGap(18, 18, 18)
                    .addComponent(resultsBox, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(109, Short.MAX_VALUE))
                .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(infoLabel)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(infoUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addContainerGap(133, Short.MAX_VALUE))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(title)
                    .addGap(33, 33, 33)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(startConnection)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(stopSearching)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(listServices))
                        .addComponent(resultsBox, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(infoLabel)
                        .addComponent(infoUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(108, 108, 108))
            );

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );

            
            this.setTitle("Blueswing Software");
            pack();
        }// </editor-fold>

        private void startConnectionActionPerformed(java.awt.event.ActionEvent evt) {
			try {
				Switcher.startClient();
				updateStatus("Search Started");
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}        	
	    	stopSearching.setEnabled(true);
	        startConnection.setEnabled(false);            // TODO add your handling code here:
        }

        private void stopSearchingActionPerformed(java.awt.event.ActionEvent evt) {
	    	try {
				Switcher.clientShutdown();
				updateStatus("Search Stopped");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	stopSearching.setEnabled(false);
	        startConnection.setEnabled(true);
	        listServices.setEnabled(true);            // TODO add your handling code here:
    }

        private void listServicesActionPerformed(java.awt.event.ActionEvent evt) {
	    	try {
				Switcher.startServicesSearch();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listServices.setEnabled(false);
	    	stopSearching.setEnabled(true);
        }
        
        //Adds a comment to the status bar when this method is called.
        private void updateStatus(String text) {
        	
            String textExisting = infoUpdate.getText();
            infoUpdate.setText("");
            infoUpdate.append(newline + text + textExisting);
            infoUpdate.selectAll();

            //Make sure the new text is visible, even if there
            //was a selection in the text area.
            infoUpdate.setCaretPosition(infoUpdate.getDocument().getLength());

        }

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
            		Logger.debug("Stated app");
            		Logger.debug("OS:" + System.getProperty("os.name") + "|" + System.getProperty("os.version") + "|"
            				+ System.getProperty("os.arch"));
            		Logger.debug("Java:" + System.getProperty("java.vendor") + " " + System.getProperty("java.version") + " " +System.getProperty("java.vm.version"));
                	//Schedule a job for the event-dispatching thread:
                    //creating and showing this application's GUI.
            		
                    new SwingtoothStart().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify
        private JPanel jPanel1;
        private JButton listServices;
        private JTextField resultsBox;
        private JButton startConnection;
        private JButton stopSearching;
        private JLabel title;
        private JLabel infoLabel;
        public JTextArea infoUpdate;
        private final static String newline = "\n";
        //public String variable = BluetoothTypesInfo.extractBluetoothAddress();
        // End of variables declaration
        
    
    
	private void quit() {
		Logger.debug("quit");
		Switcher.clientShutdown();
		Switcher.serverShutdown();
		Switcher.serverShutdownOnExit();

		Rectangle b = this.getBounds();
		Configuration.storeData("main.x", String.valueOf(b.x));
		Configuration.storeData("main.y", String.valueOf(b.y));
		Configuration.storeData("main.height", String.valueOf(b.height));
		Configuration.storeData("main.width", String.valueOf(b.width));

		//Logger.removeAppender(this);
		BlueCoveSpecific.removeAppender();

		// this.dispose();
		System.exit(0);
	}
    
}

